package org.jeecg.modules.product.risk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.product.risk.entity.TbBaseCheckRecord;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckRecordMapper;
import org.jeecg.modules.product.risk.service.ITbBaseCheckRecordService;
import org.jeecg.modules.product.risk.vo.ReportedBaseCheckRecord;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 检查情况记录
 * @Author: jeecg-boot
 * @Date:   2024-05-17
 * @Version: V1.0
 */
@Service
@Slf4j
public class TbBaseCheckRecordServiceImpl extends ServiceImpl<TbBaseCheckRecordMapper, TbBaseCheckRecord> implements ITbBaseCheckRecordService, Job {
	/*企业编码*/
//	@Value("${doubleControl.companyCode}")
//	private String companyCode;
//	@Value("${doubleControl.uploadCheckRecordUrl}")
//	private String uploadCheckRecordUrl;
	@Autowired
	private TbBaseCheckRecordMapper tbBaseCheckRecordMapper;
	@Autowired
	//private FeedUploadBatchLogMapper feedUploadBatchLogMapper;
	private static int count = 500;
	
	@Override
	public List<TbBaseCheckRecord> selectByMainId(String mainId) {
		return tbBaseCheckRecordMapper.selectByMainId(mainId);
	}

	@Override
	public int queryBaseCheckRecordCountByUserName(String username) {
		return tbBaseCheckRecordMapper.queryBaseCheckRecordCountByUserName(username);
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info("-----------专项排查任务数据开始同步双重预防系统---------------------------------------------");
		try {
			List<ReportedBaseCheckRecord> list = assembleReportedVo();
			log.info(list.size()+"--------------------查询到的排查记录数量为：---------------------"+list);
			if(!list.isEmpty()){
				printList(list);
			}
		}catch (Exception e){
			log.error("专项排查任务数据上传应急部异常--------------"+e);
		}
	}

	private List<ReportedBaseCheckRecord> assembleReportedVo() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		Date date =new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		/*在当前时间上减去天数*/
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		Date firstDays = calendar.getTime();
		List<ReportedBaseCheckRecord> list = new ArrayList<>();
		List<TbBaseCheckRecord> recordList = tbBaseCheckRecordMapper.selectList(new QueryWrapper<>(new TbBaseCheckRecord()).between("check_date",sdf.format(firstDays),sdf.format(date)).isNotNull("result_status"));
		if (!recordList.isEmpty()) {
			for (TbBaseCheckRecord entity : recordList) {
				ReportedBaseCheckRecord reported = new ReportedBaseCheckRecord();
				reported.setId(entity.getId());
				reported.setCompanyCode(entity.getCompanyCode());
				reported.setHazardCode(entity.getHazardCode());
				reported.setTaskId(entity.getTaskId());
				reported.setCheckType(entity.getCheckType());
				reported.setCheckDate(sdf2.format(entity.getCheckDate()));
				reported.setDeleted(entity.getDeleted());
				reported.setCreateBy(entity.getCreateBy());
				reported.setCreateDate(sdf.format(entity.getCreateTime()));
				reported.setUpdateBy("admin");
				reported.setUpdateDate(sdf.format(entity.getUpdateTime()));
				list.add(reported);
			}
		}
		return list;
	}

	private void printList(List<ReportedBaseCheckRecord> list) {
		List<ReportedBaseCheckRecord> logList = new ArrayList<>();
		if (!list.isEmpty()){
			for (int i = 0; i < list.size();i++) {
				logList.add(list.get(i));
				if (logList.size() == count || i == list.size() - 1){
//					HashMap<String,String> map = uploadingDoubleControlUtil.updateReporteData(JSON.toJSONString(logList),uploadCheckRecordUrl);
//
//					addLog(map);
//					logList.clear();
				}
			}
		}
	}

//	public void addLog(HashMap<String,String> resData){
//		JSONObject resultJson = JSON.parseObject(resData.get("result"));
//		String code = resultJson.getString("code");
//		String msg = resultJson.getString("msg");
//		String obj = resultJson.getString("obj");
//		FeedUploadBatchLog log = new FeedUploadBatchLog();
//		log.setStatus(code);
//		log.setErrorContent(msg);
//		log.setBatch(obj);
//		log.setType("9");
//		log.setAuthorizationHeader(resData.get("authorization"));
//		feedUploadBatchLogMapper.insert(log);
//	}
}
